package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	//test1()
	//test2()
	//test3()
	//initArrayTest()
	//exercise1()
	//exercise2()
	exercise03()
}

func test1() {
	/*
		养鸡场有6只鸡，体重分别是5、6.5、7、8、50kg。
		请问六只鸡的总体重和平均体重
	*/
	hen1 := 5.0
	hen2 := 6.5
	hen3 := 7.0
	hen4 := 8.0
	hen5 := 50.0

	totalWeight := hen1 + hen2 + hen3 + hen4 + hen5
	avgWeight := fmt.Sprintf("%.2f", totalWeight/5)

	fmt.Printf("totalWeight: %v, avgWeight: %v\n", totalWeight, avgWeight)
}

func test2() {

	// 第一个种定义数组的方式，这种方法的数组地址和首个元素地址一致
	// 1. 定义一个数组
	var hens [5]float64
	// 2. 给数组的元素赋值
	hens[0] = 5.0
	hens[1] = 6.5
	hens[2] = 7.0
	hens[3] = 8.0
	hens[4] = 50.0

	// 第二个种定义数组的方式，这种方法的数组地址和首个元素地址不一定一致
	var chickens = []float64{5.0, 6.5, 7.0, 8.0, 50.0}

	totalWeight := 0.0
	for _, v := range hens {
		totalWeight += v
	}
	avgWeight := fmt.Sprintf("%.2f", totalWeight/float64(len(hens)))
	fmt.Printf("totalWeight: %v, avgWeight: %v\n", totalWeight, avgWeight)
	fmt.Printf("array hens address: %p， first hen address: %p\n", &hens, &hens[0])
	fmt.Printf("array chickens address: %p， first chicken address: %p\n", &chickens, &chickens[0])
}

func test3() {
	var scores [5]float64
	for i := 0; i < len(scores); i++ {
		fmt.Printf("请输入成绩 %d : \n", i+1)
		fmt.Scanf("%f", &scores[i])
	}
	// 遍历数组的第一种写法
	for i := 0; i < len(scores); i++ {
		fmt.Printf("score %d = %.1f\n", i+1, scores[i])
	}
	fmt.Println("--------------------------------")
	// 遍历数组的第二种写法
	for i, score := range scores {
		fmt.Printf("score %d = %.1f\n", i+1, score)
	}
}

func initArrayTest() {
	var arr1 [3]int = [3]int{1, 2, 3}
	var arr2 = [3]int{1, 2, 3}
	var arr3 = [...]int{6, 7, 8}
	var arr4 = [3]int{1: 1, 0: 2}
	fmt.Println("arr1 =", arr1)
	fmt.Println("arr2 =", arr2)
	fmt.Println("arr3 =", arr3)
	fmt.Println("arr4 =", arr4)
}

func exercise1() {
	// 创建一个byte类型的26个元素的数组，分别放置'A'-'Z'。
	// 使用for循环访问所有元素并打印出来
	var letters [26]byte
	for i := 0; i < 26; i++ {
		letters[i] = 'A' + byte(i)
	}
	for _, c := range letters {
		fmt.Printf("%c ", c)
	}
}

func exercise2() {
	// 求出一个数组的最大值，并得到对应的下标
	var max int
	var maxIndex int
	var arr = [...]int{65, 6, 7, 6873457, 45, 34566, 234674, 53453, 453245, 623, 4523, 45, 2345, 23, 7, 8}
	for i, v := range arr {
		if v > max {
			max = v
			maxIndex = i
		}
	}
	fmt.Printf("max: %d, maxIndex: %d\n", max, maxIndex)
}

func exercise03() {
	// 随机生成5个数，并反转打印
	var arr [5]int
	var arrLen = len(arr)
	r := rand.New(rand.NewSource(time.Now().UnixNano()))
	for i := 0; i < arrLen; i++ {
		arr[i] = r.Intn(100) // 0 <= 0 < 100
	}
	fmt.Println(arr)
	for i := 0; i < arrLen/2; i++ {
		arr[arrLen-1-i], arr[i] = arr[i], arr[arrLen-1-i]
	}
	fmt.Println(arr)
}
